package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckItemImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper mapper;


    //分页条件查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page<CheckItem> p = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckItem> checkItemPage = mapper.selectPage(p, null);
        if (queryPageBean.getQueryString() != null){
            LambdaQueryWrapper<CheckItem> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CheckItem::getCode,queryPageBean.getQueryString()).or().like(CheckItem::getName,queryPageBean.getQueryString());
            Page<CheckItem> iPage = mapper.selectPage(p,wrapper);
            return new PageResult(iPage.getTotal(),iPage.getRecords());
        }
        return new PageResult(checkItemPage.getTotal(),checkItemPage.getRecords());
    }

    //添加
    @Override
    public Result add(CheckItem checkItem) {
        if (checkItem == null){
            return new Result(false,"参数异常");
        }
        int count = mapper.insert(checkItem);
        if (count <=0){
            return new Result(false,"添加失败");
        }
        return new Result(true,"新增检查项成功",null);
    }

    //删除检查项
    @Override
    public Result delete(Integer id) {
        int count = mapper.deleteById(id);
        if (count<=0){
            return new Result(false,"删除失败");
        }
        return new Result(true,"删除检查项成功",null);
    }

    //编辑回显检查项
    @Override
    public Result findById(Integer id) {
        CheckItem checkItem = mapper.selectById(id);
        return new Result(true,"查询检查项成功",checkItem);
    }

    @Override
    public Result update(CheckItem checkItem) {
        if (checkItem == null){
            return new Result(false,"参数异常");
        }
        int count = mapper.updateById(checkItem);
        if (count<=0){
            return new Result(false,"编辑失败");
        }
        return new Result(true,"编辑检查项成功");
    }

    //查询所有检查项
    @Override
    public Result findAll() {
        List<CheckItem> checkItems = mapper.selectList(null);
        return new Result(true,"查询检查项成功",checkItems);
    }

}
